﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Runtime.Tracking;
using BneyBaruch.Ingest.Core.ServiceModel;
using System.ServiceModel;

namespace BneyBaruch.Ingest.Core.WorkflowCommunication.Tracking
{
    public class WorkflowMonitoringTrackingChannel : TrackingChannel
    {
        #region Fields

        private TrackingParameters parameters = null;
        private readonly IWorkflowTrackingService workflowTrackingService = null;

        #endregion

        #region Constructors

        protected WorkflowMonitoringTrackingChannel()
        {
        }

        public WorkflowMonitoringTrackingChannel(IWorkflowTrackingService workflowTrackingService,TrackingParameters parameters)
        {
            this.workflowTrackingService = workflowTrackingService;
            this.parameters = parameters;
        }

        #endregion

        #region TrackingChannel Overrides

        protected override void InstanceCompletedOrTerminated()
        {
        }

        protected override void Send(TrackingRecord record)
        {
            // filter on record type
            if (record is WorkflowTrackingRecord)
            {
                WriteWorkflowTrackingRecord((WorkflowTrackingRecord)record);
            }
            if (record is ActivityTrackingRecord)
            {
                WriteActivityTrackingRecord((ActivityTrackingRecord)record);
            }
            if (record is UserTrackingRecord)
            {
                WriteUserTrackingRecord((UserTrackingRecord)record);
            }
        }

        #endregion

        #region Private Write Tracking Record Methods

        private void WriteUserTrackingRecord(UserTrackingRecord userTrackingRecord)
        {
            workflowTrackingService.PublishTrackRecordEvent
            (
                new WorkflowTrackRecordEvent()
                {
                    EventDateTime = userTrackingRecord.EventDateTime,
                    Message = string.Format("User tracking occured on activity {0} - user data : {1}", userTrackingRecord.ActivityType, userTrackingRecord.UserData)
                }
            );
        }

        private void WriteActivityTrackingRecord(ActivityTrackingRecord activityTrackingRecord)
        {
            workflowTrackingService.PublishTrackRecordEvent
            (
                new WorkflowTrackRecordEvent()
                {
                    EventDateTime = activityTrackingRecord.EventDateTime,
                    Message = string.Format("Activity {0} going to {1} status", activityTrackingRecord.ActivityType,activityTrackingRecord.ExecutionStatus)
                }
            );
        }

        private void WriteWorkflowTrackingRecord(WorkflowTrackingRecord workflowTrackingRecord)
        {
            workflowTrackingService.PublishTrackRecordEvent
            (
                new WorkflowTrackRecordEvent()
                {
                    EventDateTime = workflowTrackingRecord.EventDateTime,
                    Message = string.Format("Workflow {0}",workflowTrackingRecord.TrackingWorkflowEvent)
                }
            );
        }

        #endregion
    }
}
